﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

using Microsoft.Xna.Framework.Media;

namespace Objects
{
    public class Bullet : BaseObject
    {
        BaseObject Owner;
        SpriteBatch spriteBatch;
        float MovementSpeed = 300;
        public Bullet(BaseObject theOwner)
        {
            this.TheObjectType = ObjectType.Bullet;
            spriteBatch = TopDownShooter.Game1.spriteBatch;
            Image = States.Gameplay.Instance.theTextureManager.GetTexture("BULLET");
            Owner = theOwner;
            Orientation = Owner.Orientation;
            Position = new Vector2(Owner.Position.X - 8, Owner.Position.Y - 2); ;
            IsActive = true;

            if (Orientation == 0)
            {
                Velocity = new Vector2(0, -MovementSpeed);
            }
            else if (Orientation == 90)
                Velocity = new Vector2(MovementSpeed, 0);
            else if (Orientation == 180)
                Velocity = new Vector2(0, MovementSpeed);
            else if (Orientation == 270)
                Velocity = new Vector2(-MovementSpeed, 0);

            CollisionRect = new Rectangle((int)Position.X, (int)Position.Y, Image.Width, Image.Height);
        }
        public override void Update(float ElapsedTime)
        {
            Position += Velocity * ElapsedTime;
            CollisionRect.X = (int)Position.X;
            CollisionRect.Y = (int)Position.Y;
        }

        public override void Render(float ElapsedTime)
        {
            spriteBatch.Draw(Image, Position, Color.White);
        }

        public override void CheckCollision(BaseObject theObject)
        {
            if (theObject.TheObjectType == ObjectType.EnemyTank)
            {
                if (CollisionRect.Intersects(theObject.CollisionRect))
                    IsActive = false;
            }
        }
    }
}